package com.nb.comsumer.controller;

import com.nb.api.model.Product;
import com.nb.api.model.User;
import com.nb.api.service.ProductServiceAPI;
import com.nb.api.service.UserServiceAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author lihaoyang
 * @date 2022/5/16
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @DubboReference
    UserServiceAPI userServiceAPI;

    @DubboReference
    ProductServiceAPI productServiceAPI;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/create")
    public String create(@RequestParam int userId,@RequestParam int productId){
        Product product = productServiceAPI.getById(productId);
        User user = userServiceAPI.getById(userId);
        log.info("product~~~~~"+product.toString());
        log.info("user~~~~~"+user.toString());
        return "OK~~~~~~";
    }

    @GetMapping("/getProduct")
    public String getProductById(Integer id){
        Product product = restTemplate.getForObject("http://localhost:20884/product/"+id,Product.class);
        log.info("产品信息：{}",product);
        return product.toString();
    }
}
